package com.sc.byahi.user.client;

import com.google.gwt.ajaxloader.client.Properties;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.sc.byahi.user.client.components.maps.InfoWindow;
import com.sc.byahi.user.client.components.maps.InfoWindowOptions;
import com.sc.byahi.user.client.components.maps.LatLng;
import com.sc.byahi.user.client.components.maps.MClickEvent;
import com.sc.byahi.user.client.components.maps.MClickHandler;
import com.sc.byahi.user.client.components.maps.MapOptions;
import com.sc.byahi.user.client.components.maps.MapWidget;
import com.sc.byahi.user.client.components.maps.Marker;

public class MapHelper {
	public static LatLng DEFAULT = LatLng.getInstance(23.16321797457312, 79.9166402220726);
	public static int DEFAULT_ZOOM = 5;
//	private static InfoWindow iw;
	// Singleton
	private static MapHelper mapHelper = new MapHelper();

	public static MapHelper getInstance() {
		return mapHelper;
	}

	public static void createAndAttachMap(MapWidget map, SimplePanel panel, String width, String height) {
		panel.clear();
		if (map == null) {
			MapOptions mapOptions = MapOptions.newInstance();
			mapOptions.setZoom(MapHelper.DEFAULT_ZOOM);
			mapOptions.setMapTypeId("ROADMAP");
			mapOptions.setCenter(MapHelper.DEFAULT);
			map = new MapWidget(mapOptions);
//			clusterer = MarkerClusterer.newInstance(map);
		} else {
			map.getJso().setZoom(MapHelper.DEFAULT_ZOOM);
			map.getJso().setCenter(MapHelper.DEFAULT);
		}
		map.setSize(width, height);		
		panel.add(map);
		map.triggerResize();
	}
	
	public void createInfoWindow(MapWidget map, LatLng position, Widget w) {
		InfoWindowOptions options = InfoWindowOptions.getInstance();
		options.setPosition(position);
		options.setContent(w);
		InfoWindow iw = InfoWindow.getInfoWindow(options);
		iw.open(map);
	}



	public static native void attachProfileId(Marker marker, String profileId, MClickHandler handler)/*-{
		// event with extra property
		var callback = function(event) {
			event.profileId = profileId;
			$entry(@com.sc.byahi.user.client.MapHelper::onCallback(Lcom/sc/byahi/user/client/components/maps/MClickHandler;Lcom/google/gwt/ajaxloader/client/Properties;)(handler, event));
		};
		return $wnd.google.maps.event.addListener(marker, 'click', callback)

	}-*/;

	public static void onCallback(MClickHandler handler, Properties event) {
		MClickEvent mapClickEvent = new MClickEvent(event);
		handler.onEvent(mapClickEvent);
	}

}
